package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.net.API;
import me.shurufa.net.BaseResponse;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Settings;
import me.shurufa.utils.Utils;

/**
 * 作者 : andy
 * 日期 : 16/3/14 14:19
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 重置密码 第二部 提交密码
 */
public class ResetPasswordSubmitActivity extends BaseActivity implements View.OnClickListener {

    private static final String ARGUMENT_CODE = "ARGUMENT_CODE";
    private static final String ARGUMENT_PHONE = "ARGUMENT_PHONE";

    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.password_edit)
    EditText password_edit;
    @Bind(R.id.confirm_password_edit)
    EditText confirm_password_edit;

    private String mCode;
    private String mPhoneNum;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_reset_pwd_submit;
        super.onCreate(savedInstanceState);
        parseArguments();
        initUI();
        initListener();
    }

    public static void initArguments(Intent in, String code, String phone) {
        in.putExtra(ARGUMENT_CODE, code);
        in.putExtra(ARGUMENT_PHONE, phone);
    }

    private void parseArguments() {
        this.mCode = getIntent().getStringExtra(ARGUMENT_CODE);
        this.mPhoneNum = getIntent().getStringExtra(ARGUMENT_PHONE);
    }

    private void initUI() {
        titleTextView.setText(getString(R.string.reset_pwd));
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(R.string.complete);
    }

    private void initListener() {
        rightText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_text:
                reqResetPwd();
                break;
        }
    }

    /**
     * 请求设置密码
     */
    private void reqResetPwd() {

        // 校验
        final String newPwd = password_edit.getText().toString();
        final String confirmNewPwd = confirm_password_edit.getText().toString();
        if (TextUtils.isEmpty(newPwd) || TextUtils.isEmpty(confirmNewPwd)
            || !confirmNewPwd.equals(newPwd)) {

            Utils.showToast(R.string.pwd_dont_match);
            return;
        }

        if (confirmNewPwd.length() < getResources().getInteger(R.integer.min_password_length)) {
            Utils.showToast(R.string.password_too_short);
            return;
        }

        new RequestServerTask<BaseResponse>(BaseResponse.class,this,getString(R.string.commit_pls_wait)){

            @Override
            protected String requestServer() {
                Map<String, Object> params = new HashMap<>();
                params.put("code", mCode);
                params.put("mobile", mPhoneNum);
                params.put("password", newPwd);
                return HttpUtil.post(API.USER_RESET_PASSWORD, params);
            }

            @Override
            protected void onSuccess(BaseResponse result) {
                Utils.showToast(R.string.success_reset_pwd);
                //删除用户 重新登录
                EventBus.getDefault().post(new Intent(Constants.ACTION_USER_CHANGE));
                Settings.delUser(String.valueOf(Global.currentUser.id));
                Global.currentUser = null;
                finishToLogin();
            }
        }.setDefaultErrMsg(R.string.commit_failed).start();
    }

    private void finishToLogin() {
        Intent in = new Intent(this, NewLoginActivity.class);
        in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(in);
        finish();
    }
}
